package simu

import (
	"fmt"
	"topobuild/utils"

	"github.com/xuri/excelize/v2"
)

/*
rsing_id	left_dev_id	left_ifindex	to_rsing_id	right_dev_id	right_ifindex	link_ifspeed
1	1	1	1	2	1	10000000000
1	1	2	1	3	1	10000000000
1	2	2	1	3	2	10000000000
*/

type TableLink struct {
	RsingId      int `json:"rsing_id"`
	LeftDevId    int `json:"left_dev_id"`
	LeftIfIndex  int `json:"left_ifindex"`
	ToRsingId    int `json:"to_rsing_id"`
	RightDevId   int `json:"right_dev_id"`
	RightIfIndex int `json:"right_ifindex"`
	LinkIfSpeed  int `json:"link_ifspeed"`
}

// 将TableLink中的数据,写入到指定xlsx文件的link表中
func WriteLinkXlsx(filename string, links []TableLink) {
	// 如果filename已经存在,则直接打开,否则创建一个新文件
	utils.CheckFile(filename)
	xlsx, err := excelize.OpenFile(filename)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer xlsx.Close()
	// Create a new sheet.
	index, err := xlsx.NewSheet("link")
	if err != nil {
		fmt.Println(err)
		return
	}
	// Set value of a cell.
	xlsx.SetCellValue("link", "A1", "rsing_id")
	xlsx.SetCellValue("link", "B1", "left_dev_id")
	xlsx.SetCellValue("link", "C1", "left_ifindex")
	xlsx.SetCellValue("link", "D1", "to_rsing_id")
	xlsx.SetCellValue("link", "E1", "right_dev_id")
	xlsx.SetCellValue("link", "F1", "right_ifindex")
	xlsx.SetCellValue("link", "G1", "link_ifspeed")
	// 设置单元格样式
	// style, _ := xlsx.NewStyle(`{"alignment":{"horizontal":"center"}}`)
	// xlsx.SetCellStyle("link", "A1", "H1", style)
	// 设置列宽
	// xlsx.SetColWidth("link", "A", "H", 20)
	// 设置行高
	// Set active sheet of the workbook.
	xlsx.SetActiveSheet(index)
	// Save xlsx file by the given path.
	for i, link := range links {
		xlsx.SetCellValue("link", fmt.Sprintf("A%d", i+2), link.RsingId)
		xlsx.SetCellValue("link", fmt.Sprintf("B%d", i+2), link.LeftDevId)
		xlsx.SetCellValue("link", fmt.Sprintf("C%d", i+2), link.LeftIfIndex)
		xlsx.SetCellValue("link", fmt.Sprintf("D%d", i+2), link.ToRsingId)
		xlsx.SetCellValue("link", fmt.Sprintf("E%d", i+2), link.RightDevId)
		xlsx.SetCellValue("link", fmt.Sprintf("F%d", i+2), link.RightIfIndex)
		xlsx.SetCellValue("link", fmt.Sprintf("G%d", i+2), link.LinkIfSpeed)
	}
	if err := xlsx.SaveAs(filename); err != nil {
		fmt.Println(err)
	}
}
